package com.block.job.service;


import cn.hutool.core.util.PhoneUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.block.common.service.IApplicationInfoService;
import com.block.common.service.IChannelCommonService;
import com.block.common.service.LogFactory;
import com.block.common.vo.ResponseVo;
import com.block.db.entity.ApplicationInfo;
import com.block.db.entity.ChannelBaseInfo;
import com.block.db.entity.MemberBaseInfo;
import com.block.sms.dto.SmsParamDTO;
import com.block.sms.service.ISmsRecordService;
import com.market.member.service.IMemberBaseInfoService;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Component
public class SmsSendJob {
    @Autowired
    IMemberBaseInfoService memberBaseInfoService;
    @Autowired
    IChannelCommonService  channelCommonService;
    @Autowired
    IApplicationInfoService applicationInfoService;
    @Autowired
    ISmsRecordService  smsRecordService;

    private static final Logger log = LogFactory.getJobLog();
    //@XxlJob("ReserveJob")
    public void reserveJob() {
        XxlJobHelper.log("查询五分钟前所有用户数据---------开始执行--------");


//        Date now = new Date();
//        Calendar calendar = Calendar.getInstance();
//        calendar.setTime(now);
//        calendar.add(Calendar.MINUTE, -5);
//        Date fiveMinutesAgo = calendar.getTime();
//
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String start = sdf.format(fiveMinutesAgo);
//        String end = sdf.format(now);
//        //五分钟前所有未实名用户
//        List<MemberBaseInfo> memberBaseInfos = memberBaseInfoService.getUnfinishedUsersWithinMinutes(start, end);
//        //五分钟前所有已实名用户
//        List<MemberBaseInfo> memberBaseInfos1 = memberBaseInfoService.getRecordsBeforeFiveMinutes(start, end);
//        //发送短信
//        for (MemberBaseInfo memberBaseInfo : memberBaseInfos) {
//            String mobile = memberBaseInfo.getMobile();
//            String regAppCode = memberBaseInfo.getRegAppCode();
//            String channelCode = memberBaseInfo.getChannelCode();
//            if (PhoneUtil.isMobile(mobile)) {
//                SmsParamDTO smsParamDTO = new SmsParamDTO();
//                smsParamDTO.setOtype("4");
//                smsParamDTO.setMobie(mobile);
//
//                // 推送，短信提醒
//                JSONArray jsonArray = new JSONArray();
//                jsonArray.add("XX");
//                smsParamDTO.setParamArray(jsonArray);
//                ChannelBaseInfo channelBaseInfo = channelCommonService.getBaseInfoByCode(channelCode);
//                ApplicationInfo applicationInfo = applicationInfoService.getAppInfoByAppCode(regAppCode);
//                System.out.println(memberBaseInfo.toString());
//                //ResponseVo responseVo = smsRecordService.sendSms(smsParamDTO, applicationInfo, channelBaseInfo);
//                //log.info("未实名用户发送提醒短信【responseVo】{}", JSON.toJSONString(responseVo));
//            }
//        }
//        for (MemberBaseInfo memberBaseInfo : memberBaseInfos1) {
//            String mobile = memberBaseInfo.getMobile();
//            String regAppCode = memberBaseInfo.getRegAppCode();
//            String channelCode = memberBaseInfo.getChannelCode();
//            if(PhoneUtil.isMobile(mobile)){
//                SmsParamDTO smsParamDTO = new SmsParamDTO();
//                smsParamDTO.setOtype("4");
//                smsParamDTO.setMobie(mobile);
//
//                // 推送，短信提醒
//                JSONArray jsonArray = new JSONArray();
//                jsonArray.add("XX");
//                smsParamDTO.setParamArray(jsonArray);
//                ChannelBaseInfo channelBaseInfo = channelCommonService.getBaseInfoByCode(channelCode);
//                ApplicationInfo applicationInfo = applicationInfoService.getAppInfoByAppCode(regAppCode);
//                System.out.println(memberBaseInfo.toString());
//                ResponseVo responseVo = smsRecordService.sendSms(smsParamDTO, applicationInfo, channelBaseInfo);
//                //log.info("已实名实名用户发送提醒短信【responseVo】{}", JSON.toJSONString(responseVo));
//            }
//        }
    }
}
